package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderSettingListService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersettinglist")
public class OrderSettingListController {
    @Reference
    private OrderSettingListService orderSettingListService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody  Map map){
        try {
            return orderSettingListService.findPage(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //调整开关状态
    @RequestMapping("/changeStatus")
    public Result chanStatus(@RequestBody Map<String, Object> map) {
        try {
            orderSettingListService.changeStatus(map);
            return new Result(true, "状态修改成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    //查询所有套餐信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Setmeal> list = orderSettingListService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //新增电话预约订单
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
       map.put("orderType", Order.ORDERTYPE_TELEPHONE);
        try {
            Result result = orderSettingListService.order(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ORDERSETTINGLIST_FAIL);
        }
    }

    //修改功能回显基本信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderSettingListService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //修改预约信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order){
        try {
            orderSettingListService.edit(order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ORDERSETTINGLIST_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ORDERSETTINGLIST_SUCCESS);
    }

    //删除预约信息
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderSettingListService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDERSETTINGLIST_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ORDERSETTINGLIST_SUCCESS);
    }
}
